/*
 * Copyright (C) Francisco Morero Peyrona. All rights reserved.
 *
 * This software is published under the terms of Open Source
 * License version 1.1, a copy of which has been included with 
 * this distribution in the License.txt file.
 */

package jdbcmanager.swing.help;

import java.awt.*;
import java.awt.event.ActionEvent;

import javax.swing.*;

/**
 * A JPanel containing in CENTER an instance of Sun's MemoryMonitor class, and in SOUTH buttons for G.C. and Start/Stop monitoring.
 * 
 * @author Francisco
 */
class MemMon extends JPanel
{
    private MemoryMonitor memmon;
    private JButton       btnRun;

    MemMon()
    {
        this.memmon = new MemoryMonitor();

        setLayout( new BorderLayout() );
        add( memmon, BorderLayout.CENTER );
        add( southPanel(), BorderLayout.SOUTH );
    }
    
    void updateButtons()
    {
        this.btnRun.setText( this.memmon.isRunning() ? "Stop" : "Start" );
    }
    
    //------------------------------------------------------------------------//

    private JPanel southPanel()
    {
        this.btnRun = new JButton( "Stop" );
             btnRun.addActionListener( new java.awt.event.ActionListener()
             {
                 public void actionPerformed( ActionEvent evt )
                 {
                     btnRun.setText( MemMon.this.memmon.toggleRun() ? "Stop" : "Start" );
                 }
             } );

        final JButton btnConfig = new JButton( "Config" );
                      btnConfig.addActionListener( new java.awt.event.ActionListener()
                      {
                          public void actionPerformed( ActionEvent evt )
                          {
                              btnConfig.setText( MemMon.this.memmon.toggleConfig() ? "Config" : "Graph" );
                              btnRun.setText( "Stop" );
                          }
                      } );

        JButton btnGC = new JButton( "Invoke G.C." );
                btnGC.addActionListener( new java.awt.event.ActionListener()
                {
                    public void actionPerformed( ActionEvent evt )
                    {
                        Runtime.getRuntime().gc();
                        Toolkit.getDefaultToolkit().beep();
                    }
                } );

        JPanel pnlSouth = new JPanel( new FlowLayout( FlowLayout.TRAILING ) );
        pnlSouth.add( btnRun );
        pnlSouth.add( btnConfig );
        pnlSouth.add( btnGC );

        return pnlSouth;
    }
}